<?php

/**
 * @author quang
 * @copyright 2011
 */
require_once("includes/functions/general.php");
require_once("includes/functions/dal.php");
//session_start();

#get Quizzes
$quizzDal = new QuizzDal();
if(!isset($_SESSION["quizzes"])){
    $_SESSION["quizzes"] = $quizzDal->selectAll();
}

    $_SESSION['quizCount'] =  count($_SESSION["quizzes"]);
#print_r($_SESSION["user"]);
$qNum = isset($_GET['qNum']) ? intval($_GET['qNum']) : 1;
$prev = $qNum-1;
$next = $qNum+1;

$ans = "";
if(isset($_POST['answer'])){
    
    $ans = $_POST['answer'] ;
    # checking the answer is correct or false, if correct + 1point;
    
    # the first index of array is 0 so we need subtract 1  
    if($_SESSION["quizzes"][$prev-1]->checkAnswer($ans)){
 #       echo "This is the correct answer ";
        $score = $_SESSION["user"]->getScore();  
        if(!isset($score)) $score = 0;      
        $_SESSION["user"]->setScore($score + 1);
        #save the user's answer in array
        $_SESSION["user"]->addAnswer(1);
        
 #       echo "Check answer of user ". $_SESSION["user"]->getAnswer();        
    }else{
        $_SESSION["user"]->addAnswer(0);
    }
    if(!isset($_SESSION['ans']))
    {
      $_SESSION['ans']= array();      
    }
    $_SESSION['ans'][$prev] = $ans;    
    
    if(isset($_SESSION['ans']) && count($_SESSION['ans']) == $_SESSION['quizCount']){
        #save user's score and answers
        $userDal = new UserDal();
        $userDal->saveScore($_SESSION["user"]);
        #end saving
  #      echo 'erase';
        unset($_SESSION['ans']);
        unset($_SESSION['quizzes']);
        
        /**
 * echo "
 *         <script type=\"text/javascript\">
 *             alert('You have ".$_SESSION["user"]->getScore()." correct answer. And you have ".$_SESSION["user"]->getScore()." points for this game');
 *             
 *             window.location= 'post_to_wall.php?langue=".$strLanguage."';            
 *         </script>";
 */
        tep_redirect("point.php?points=".$_SESSION["user"]->getScore());
    }
}

if($prev <= 1) $prev = 1;

if($next > (intval($_SESSION['quizCount'])+1)) $next = $_SESSION['quizCount'];
   
if(isset($_SESSION['ans'])){
    #print_r($_SESSION['ans']);
    #echo " ;answer array:".count($_SESSION['ans'])." ; ";
    #echo   "; quiz coount: ". $_SESSION['quizCount'];   
    
    #echo "THe current question number ".$qNum. " and prev is ".$prev;         
}

$default_action = tep_embed_langue_url($_SERVER['PHP_SELF']."?qNum=". $next);


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head >
        <?php require_once("includes/heading.php"); ?>
    </head>
    <body >
        <div id="wrap">
            <?php require_once("includes/top.php"); ?>
            <div id="content">                
                <div class="main">
                    <p class="question"><!--
Esprere thing for Independence day, do you choose it?
--> <?php
                    if($strLanguage == 'en'){
                        echo $_SESSION["quizzes"][$qNum-1]->getQuizEn();
                    }else{
                        echo $_SESSION["quizzes"][$qNum-1]->getQuizFr();
                    }
                      
     ?>
                    </p>
                    
                    <div class="product">
                       <img src="images/tintamar/image_3_606-200x200.png" alt="trefdritb" width="60" height="60" />
                    </div>
                    <a class="prev" href="quizz2.php?qNum=<?php echo $prev; ?>">Previous</a>  
                    <form id="questions" method="post" action="<?php echo tep_encode_specialchars($default_action) ;?>">
                    
                    <?php
                        require_once('loadQuestion.php');
                    ?>

                    </form>
                    <!--
<a class="next" href="quizz2.php?qNum=<?php #echo $next; ?>">Next</a>
-->
                    <div class="clear"></div>
                </div>
                
           </div>
           <?php require_once("includes/bottom.php"); ?>
        </div>       
  </body>
</html>